package DAO;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import hibernate.RequisitionInfo;
import hibernateUtil.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class RequisitionInfoDAO {
	
	public void createRequisitionInfo(String department,String proposer,String time,String deviceName,
			                          String model,String produceUnit,String quantity,String money,String reason,
			                          String purchaseCondition,String personCondition,String economyBenefit,
			                          String useRate,String nowStatus,String remark)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		RequisitionInfo requisition = new RequisitionInfo();
		Date time_d = java.sql.Date.valueOf(time);
		int quantity_i = Integer.parseInt(quantity);
		double money_d = Double.parseDouble(money);
		double economyBenefit_d = Double.parseDouble(economyBenefit);
		double useRate_d = Double.parseDouble(useRate);
		
		requisition.setDepartment(department);
		requisition.setProposer(proposer);
		requisition.setTime(time_d);
		requisition.setDeviceName(deviceName);
		requisition.setModel(model);
		requisition.setProduceUnit(produceUnit);
		requisition.setQuantity(quantity_i);
		requisition.setMoney(money_d);
		requisition.setReason(reason);
		requisition.setPurchaseCondition(purchaseCondition);
		requisition.setPersonCondition(personCondition);
		requisition.setEconomyBenefit(economyBenefit_d);
		requisition.setUseRate(useRate_d);
		requisition.setNowStatus(nowStatus);
		requisition.setRemark(remark);
		requisition.setApproveOpinion("不同意");
		requisition.setApproveRemark("待审批");
		
		session.save(requisition);
		trans.commit();
	}
	
	public void updateRequisitionInfo(String id,String department,String proposer,String time,String deviceName,
            String model,String produceUnit,String quantity,String money,String reason,
            String purchaseCondition,String personCondition,String economyBenefit,
            String useRate,String nowStatus,String remark)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query q = session.createQuery("update RequisitionInfo set department=:department,proposer=:proposer,time=:time,deviceName=:deviceName,model=:model,produceUnit=:produceUnit,quantity=:quantity,money=:money,reason=:reason,purchaseCondition=:purchaseCondition,personCondition=:personCondition,economyBenefit=:economyBenefit,useRate=:useRate,nowStatus=:nowStatus,remark=:remark where id=:id");
		q.setString("department",department);
		q.setString("proposer",proposer);
		q.setString("time",time);
		q.setString("deviceName",deviceName);
		q.setString("model",model);
		q.setString("produceUnit",produceUnit);
		q.setString("quantity",quantity);
		q.setString("money",money);
		q.setString("reason",reason);
		q.setString("purchaseCondition",purchaseCondition);
		q.setString("personCondition",personCondition);
		q.setString("economyBenefit",economyBenefit);
		q.setString("useRate",useRate);
		q.setString("nowStatus",nowStatus);
		q.setString("remark",remark);
		q.setString("id",id);
		
		q.executeUpdate();
		trans.commit();
	}
	
	@SuppressWarnings("unchecked")
	public Vector searchRequisitionInfo(String start,String limit,String department,String proposer,String time1,String time2,
			                          String deviceName,String produceUnit)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		Vector vt = new Vector();
		
		String str = "from RequisitionInfo where ";
		
		if(!"".equals(department))
		{
			str = str + "department=:department ";
		}
		if(!"".equals(proposer))
		{
			if(!"".equals(department))
			{
				str = str + "or proposer=:proposer ";
			}
			else
			{
				str = str + "proposer=:proposer ";
			}
		}
		if(!"".equals(deviceName))
		{
			if(!"".equals(department) || !"".equals(proposer))
			{
				str = str + "or deviceName=:deviceName ";
			}
			else
			{
				str = str + "deviceName=:deviceName ";
			}
		}
		if(!"".equals(produceUnit))
		{
			if(!"".equals(department) || !"".equals(proposer) || !"".equals(deviceName))
			{
				str = str + "or produceUnit=:produceUnit ";
			}
			else
			{
				str = str + "produceUnit=:produceUnit ";
			}
		}
		if((!"".equals(time1)) && (!"".equals(time2)))
		{
			if(!"".equals(department) || !"".equals(proposer) || !"".equals(deviceName) || !"".equals(produceUnit))
			{
				str = str + "or time between:time1_d and:time2_d ";
			}
			else
			{
				str = str + "time between:time1_d and:time2_d ";
			}
		}
		else if(!"".equals(time1))
		{
			if(!"".equals(department) || !"".equals(proposer) || !"".equals(deviceName) || !"".equals(produceUnit))
			{
				str = str + "or time=:time1_d ";
			}
			else
			{
				str = str + "time=:time1_d ";
			}			
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(department))
		{
			query.setString("department", department);
		}
		if(!"".equals(proposer))
		{
			query.setString("proposer", proposer);
		}
		if(!"".equals(deviceName))
		{
			query.setString("deviceName", deviceName);
		}
		if(!"".equals(deviceName))
		{
			query.setString("produceUnit", produceUnit);
		}
		if(!"".equals(time1))
		{
			Date time1_d = java.sql.Date.valueOf(time1);
			query.setDate("time1_d", time1_d);
		}
		if(!"".equals(time2))
		{
			Date time2_d = java.sql.Date.valueOf(time2);
			query.setDate("time2_d", time2_d);
		}		
		
		int count = (query.list()).size();
		vt.add(count);
		
		query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；

		List l = query.list();
		vt.add(l);
		trans.commit();
		
		return vt;
	}
	

	@SuppressWarnings("unchecked")
	public Vector getAll(String start,String limit)
	{
		Vector vt = new Vector();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query query = session.createQuery("from RequisitionInfo");
		int count = (query.list()).size();       //得到查询总数目；
		vt.add(count);
		
		query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；
		
		List l = query.list();               //查询总数目中的limit条记录出来；
		vt.add(l);
		trans.commit();	
		return vt;
	}
	
    @SuppressWarnings("unchecked")
    /*
     * 通过购置申请编号（assetNo）删除购置申请信息
     */
	public void deleteAssetGzsq(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			RequisitionInfo assetRequisitionInfo = (RequisitionInfo)session.get(RequisitionInfo.class, Integer.parseInt(assetNo.elementAt(i).toString()));
			session.delete(assetRequisitionInfo);
		}
		
		trans.commit();
    } 
    
    /*
     * 购置审批
     */
    public void gzsp(String id , String approveOpinion , String remark)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		RequisitionInfo requisitionInfo = (RequisitionInfo)session.get(RequisitionInfo.class, Integer.parseInt(id));
		requisitionInfo.setApproveOpinion(approveOpinion);
		requisitionInfo.setRemark(remark);
		session.update(requisitionInfo);
		
		trans.commit();
    }
}
